/* gm_guestbook.js.php <?php
#   --------------------------------------------------------------
#   gm_guestbook.js.php 2011-01-24 gambio
#   Gambio GmbH
#   http://www.gambio.de
#   Copyright (c) 2011 Gambio GmbH
#   Released under the GNU General Public License
#   --------------------------------------------------------------
?>*/

$(document).ready(function(){

	$("#guestbook_send_entry").click(function(){
				gm_check_entry();
			}
		);
	
	gm_guestbook_get_entries(0);
	
});


function gm_guestbook_get_entries(limit){
	$('#gm_guestbook_success_message').html('');
	if($('#gm_guestbook_entries').html() == '') $('#gm_guestbook_entries').html('<img src="<?php echo DIR_WS_IMAGES; ?>loading.gif" />');
	$('#gm_guestbook_entries').load('gm_ajax.php?module=guestbook&limit=' + limit);
	gm_guestbook_limit = limit;
}

function gm_check_entry(){
	$('#gm_guestbook_error_message').load('gm_ajax.php?module=guestbook&check=1' 
																	+ '&name=' + escape($('#gm_guestbook_name').val())
																	+ '&message=' + escape($('#gm_guestbook_message').val())
																	+ '&vvcode=' + $('#gm_guestbook_vvcode').val(),
			function(errors){
				if(errors == '') gm_send_entry();
				else{
					$('#gm_guestbook_success_message').html('');
					$('#gm_guestbook_vvcode').val('');
					$('#gm_guestbook_vvcode_image').html('<img src="gm_create_vvcodes.php?rand=' + Math.random() + '&XTCsid=' + gm_session_id + '" alt="" />');
				}
			}
	);
}

function gm_send_entry(){
	if($('#gm_guestbook_name').val() != '' && $('#gm_guestbook_message').val() != ''){
		$('#gm_guestbook_entries').html('<img src="<?php echo DIR_WS_IMAGES; ?>loading.gif" />');
		$('#gm_guestbook_entries').load('gm_ajax.php?module=guestbook' 
																		+ '&name=' + escape($('#gm_guestbook_name').val())
																		+ '&email=' + escape($('#gm_guestbook_email').val())
																		+ '&homepage=' + escape($('#gm_guestbook_homepage').val())
																		+ '&message=' + escape($('#gm_guestbook_message').val())
																		+ '&vvcode=' + $('#gm_guestbook_vvcode').val(),
				function(){
					$('#gm_guestbook_error_message').html('');
					$('#gm_guestbook_email').val('');
					$('#gm_guestbook_homepage').val('');
					$('#gm_guestbook_message').val('');
					$('#gm_guestbook_vvcode').val('');
					$('#gm_guestbook_vvcode_image').html('<img src="gm_create_vvcodes.php?rand=' + Math.random() + '&XTCsid=' + gm_session_id + '" alt="" />');
					<?php if(gm_get_conf('GM_GUESTBOOK_ACTIVATE_ENTRIES') == 'true'){?>
					$('#gm_guestbook_success_message').html('<?php echo str_replace("'", "\'", GM_GUESTBOOK_SUCCESS_1); ?>');
					<?php }else{ ?>
					$('#gm_guestbook_success_message').html('<?php echo str_replace("'", "\'", GM_GUESTBOOK_SUCCESS_2); ?>');
					<?php } ?>
					window.scrollTo(0, 0);
				}
		);
	}
}

function gm_delete_entry(id){
	if(confirm("<?php echo GM_GUESTBOOK_DELETE_QUESTION; ?>") == true){
		$('#gm_guestbook_entries').load('gm_ajax.php?module=guestbook&delete=' + id + '&limit=' + gm_guestbook_limit);
		$('#gm_guestbook_success_message').html('');
	}
}

function gm_activate_entry(id){
	$('#gm_guestbook_entries').load('gm_ajax.php?module=guestbook&activate=' + id + '&limit=' + gm_guestbook_limit);
	$('#gm_guestbook_success_message').html('');
}

function gm_deactivate_entry(id){
	$('#gm_guestbook_entries').load('gm_ajax.php?module=guestbook&deactivate=' + id + '&limit=' + gm_guestbook_limit);
	$('#gm_guestbook_success_message').html('');
}

var gm_guestbook_limit = 0;